package net.uman.web.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uman.framework.modules.orm.Page;
import net.uman.framework.modules.orm.PropertyFilter;
import net.uman.framework.modules.security.springsecurity.SpringSecurityUtils;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.entity.Point;
import net.uman.entity.Topic;
import net.uman.entity.Bbsplate;
import net.uman.entity.User;
import net.uman.service.BbsplateManager;
import net.uman.service.PointManager;
import net.uman.service.TopicManager;
import net.uman.service.UserManager;
import net.uman.util.Constants;
import net.uman.web.CrudActionSupport;
@Namespace("/manager")
public class BbsAction extends CrudActionSupport<Topic> {
	private static final long serialVersionUID = 4373425860945767562L;

	@Autowired
	private BbsplateManager bbsplateManager;
	@Autowired
	private TopicManager topicManager;
	@Autowired
	private UserManager userManager;
	@Autowired
	private PointManager pointManager;
	
	public int type = Constants.BBS_TYPE_PLATE;
	
	public String title;
	public Long userId;
	public String description;
	public Long plateId;
	public Integer addPoint;
	public Integer commentsPoint;
	public Integer hotPoint;
	public Integer qualityPoint;
	public Long topicId;
	
	public Topic getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		if(type==Constants.BBS_TYPE_PLATE)
			return "plate";
		else if(type==Constants.BBS_TYPE_TOPIC)
			return "topic";
		else return SUCCESS;
	}

	public void plateData(){
		Page<Bbsplate> bbsplates = getPlatePage();
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject jObject = new JSONObject();
		for(Bbsplate bbsplate : bbsplates.getResult()){
			jObject.put("code", bbsplate.getCode());
			jObject.put("id", bbsplate.getId());
			jObject.put("title", bbsplate.getTitle());
			jObject.put("description", bbsplate.getDescription());
			jObject.put("userName", bbsplate.getAddUserName());
			jsonArray.add(jObject);
		}
		jsonObject.put("rows", jsonArray);
		jsonObject.put("total", bbsplates.getTotalCount());
		Struts2Utils.renderJson(jsonObject);
	}

	private Page<Bbsplate> getPlatePage() {
		Page<Bbsplate> bbsplates = new Page<Bbsplate>(10);
		bbsplates.setPageNo(pageNum);
		bbsplates = bbsplateManager.findBbsplatePage(bbsplates, null);
		return bbsplates;
	}
	
	public void topicData(){
		Page<Topic> topicPage = getTopicPage();
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject jObject = new JSONObject();
		for(Topic topic : topicPage.getResult()){
			jObject.put("id", topic.getId());
			jObject.put("title", topic.getTitle());
			jObject.put("authorName", topic.getAuthorName());
			jObject.put("addDate", topic.getAddDate());
			jObject.put("content", topic.getContent());
			jObject.put("hits", topic.getHits());
			jObject.put("review", topic.getReview());
			jObject.put("plateName", topic.getPlateName());
			jObject.put("status", topic.getStatus());
			jsonArray.add(jObject);
		}
		jsonObject.put("rows", jsonArray);
		jsonObject.put("total", topicPage.getTotalCount());
		Struts2Utils.renderJson(jsonObject);
	}
	
	
	private Page<Topic> getTopicPage() {
		Page<Topic> topicPage = new Page<Topic>(10);
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		if(Struts2Utils.getParameter("plateId")!=null){
			filters.add(new PropertyFilter("filter_EQL_plateId",Struts2Utils.getParameter("plateId")));
		}
		filters.add(new PropertyFilter("filter_EQI_type",String.valueOf(Constants.TOPIC_TYPE_ROOT)));
		return topicManager.findTopicPage(topicPage,filters);
	}
	
	public String addPlate(){
		return "addplate";
	}
	
	public String modPlate(){
		if(plateId!=null){
			Bbsplate bbsplate = bbsplateManager.getBbsplate(plateId);
			title = bbsplate.getTitle();
			description = bbsplate.getDescription();
			plateId = bbsplate.getId();
		}
		return "addplate";
	}
	
	public void delPlate(){
		if(plateId!=null)
			bbsplateManager.deletePlate(plateId);
	}
	
	public String setPoint(){
		Point point = pointManager.getPoint();
		if(point!=null){
			addPoint = point.getAddPoint();
			commentsPoint = point.getCommentsPoint();
			hotPoint = point.getHotPoint();
			qualityPoint = point.getQualityPoint();
		}
		return "setpoint";
	}

	public void savePlate(){
		if(!StringUtils.isEmpty(description)&&
				!StringUtils.isEmpty(title)){
			Bbsplate bbsplate = new Bbsplate();
			Date date =  new Date(System.currentTimeMillis());
			if(plateId!=null){
				bbsplate.setId(plateId);
			}else{
				bbsplate.setAddUserId(SpringSecurityUtils.getCurrentUser().getId());
				bbsplate.setAddUserName(SpringSecurityUtils.getCurrentUser().getName());
				bbsplate.setAddDate(new Date());
			}
				
			bbsplate.setAddDate(date);
			bbsplate.setCode("BK"+String.valueOf(date.getTime()).substring(5));
			bbsplate.setDescription(description);
			bbsplate.setTitle(title);
			bbsplate.setAddUserId(SpringSecurityUtils.getCurrentUser().getId());
			bbsplate.setAddUserName(SpringSecurityUtils.getCurrentUser().getName());
			bbsplateManager.save(bbsplate);
		}
	}
	
	public void plateOwnerData(){
		List<User> users = userManager.getOperatorList();
		JSONArray jsonArray = new JSONArray();
		for(User user : users){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", user.getId());
			jsonObject.put("text", user.getName());
			jsonArray.add(jsonObject);
		}
		Struts2Utils.renderJson(jsonArray);
	}
	
	public void savePoint(){
		if(addPoint!=null&&commentsPoint!=null&&hotPoint!=null&&qualityPoint!=null){
			Point point = pointManager.getPoint()!=null?pointManager.getPoint():new Point();
			point.setAddPoint(addPoint);
			point.setCommentsPoint(commentsPoint);
			point.setHotPoint(hotPoint);
			point.setQualityPoint(qualityPoint);
			pointManager.save(point);
			Struts2Utils.renderText("设置完成！");
		}else{
			Struts2Utils.renderText("设置失败！");
		}
	}
	
	/**
	 * 回帖查询
	 * @return
	 * @author jinlong-dt
	 */
	public String reviewTopic(){
		return "reviewtopic";
	}
	
	/**
	 * 回帖查询
	 * @return
	 * @author jinlong-dt
	 */
	public void reviewTopicData(){
		Page<Topic> topicPage = new Page<Topic>(10);
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter("filter_EQL_parentId",topicId.toString()));
		filters.add(new PropertyFilter("filter_EQI_type",String.valueOf(Constants.TOPIC_TYPE_COMMENTS)));
		topicManager.findTopicPage(topicPage,filters);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject jObject = new JSONObject();
		for(Topic topic : topicPage.getResult()){
			jObject.put("id", topic.getId());
			jObject.put("title", topic.getTitle());
			jObject.put("authorName", topic.getAuthorName());
			jObject.put("addDate", topic.getAddDate());
			jObject.put("content", topic.getContent());
			jObject.put("hits", topic.getHits());
			jObject.put("review", topic.getReview());
			jObject.put("plateName", topic.getPlateName());
			jObject.put("status", topic.getStatus());
			jsonArray.add(jObject);
		}
		jsonObject.put("rows", jsonArray);
		jsonObject.put("total", topicPage.getTotalCount());
		Struts2Utils.renderJson(jsonObject);
	}
	
	/**
	 * 回帖删除
	 */
	public void deleteReviewTopic(){
		try{
			topicManager.deleteTopic(topicId);
			Struts2Utils.renderText("删除成功");
		}catch(Exception e){
			Struts2Utils.renderText("删除失败");
		}
	}
	
	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}

}
